//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LitwareHR.PublicApp.Presenters.Careers.Recruiting;
using LitwareHR.Recruiting.Contract;
using System.Threading;
using System.Globalization;
using Shp.Runtime.Contract;
using System.Collections.Generic;

public partial class Careers_Recruiting_Resume : BasePage, IResumeView
{
    ResumePresenter _presenter;

    public ResumePresenter Presenter
    {
        get
        {
            return _presenter;
        }
    }

    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);
        _presenter = new ResumePresenter(this);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies[Constants.PositionCookie] == null)
            throw new ArgumentNullException(Constants.PositionCookie);
        string positionId = Request.Cookies[Constants.PositionCookie].Value;
        if (string.IsNullOrEmpty(positionId))
            throw new ArgumentNullException(Constants.PositionCookie);

        _presenter.OnViewLoaded(Helpers.TenantAlias);
    }

    public void BuildEntityForm(EntityField[] list)
    {
        foreach (EntityField field in list)
        {
            if (field.IsVisibleFromApplicationForm)
            {
                FormTable.Rows.Add(CreateTableRow(field));
            }
        }
    }

    private TableRow CreateTableRow(EntityField field)
    {
        TableRow row = new TableRow();

        TableCell labelCell = new TableCell();
        labelCell.Text = field.Name;        
        TableCell ctrlCell = new TableCell();
        TextBox textBox = new TextBox();
        textBox.ID = field.Name+"CustomFieldTextBox";
        ctrlCell.Controls.Add(textBox);
        RequiredFieldValidator fieldValidator = new RequiredFieldValidator();
        fieldValidator.ControlToValidate = textBox.ID;
        fieldValidator.ErrorMessage = field.Name + " is required.";
        ctrlCell.Controls.Add(fieldValidator);
        HiddenField isShared = new HiddenField();
        isShared.Value = field.IsShared.ToString();
        ctrlCell.Controls.Add(isShared);
        HiddenField typeName = new HiddenField();
        typeName.Value = field.TypeName;
        ctrlCell.Controls.Add(typeName);        
        row.Cells.Add(labelCell);
        row.Cells.Add(ctrlCell);
        
        return row;
    }

    public void SetValidators()
    {
    }

    public void SetDefaultValues()
    {
    }

    public void ShowVideoPanel(bool visible)
    {
        ResumeVideoFieldSet.Visible = visible;
    }

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        if (Request.Cookies[Constants.PositionCookie] == null)
            throw new ArgumentNullException(Constants.PositionCookie);
        string positionId = Request.Cookies[Constants.PositionCookie].Value;
        if (string.IsNullOrEmpty(positionId))
            throw new ArgumentNullException(Constants.PositionCookie);

        Page.Validate();

        if (Page.IsValid)
        {
            List<EntityField> list = new List<EntityField>();

            foreach (TableRow row in FormTable.Rows)
            {
                EntityField field = new EntityField();
                field.Name = row.Cells[0].Text;

                Control ctrl = row.Cells[1].Controls[0];
                Control ctrlTypeName = row.Cells[1].Controls[2];
                if (ctrl != null)
                {
                    String typeName = ((HiddenField)ctrlTypeName).Value;
                    if (typeName == "Date and time")
                    {
                        string dateFieldText = ((TextBox)ctrl).Text;
                        DateTime dateField;
                        if (DateTime.TryParse(dateFieldText, CultureInfo.CurrentCulture, DateTimeStyles.AssumeLocal, out dateField))
                        {
                            field.Value = ((TextBox)ctrl).Text;
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        field.Value = ((TextBox)ctrl).Text;
                    }
                }

                Control ctrlIsShared = row.Cells[1].Controls[2];
                if (ctrlIsShared != null)
                {
                    field.IsShared = Convert.ToBoolean(((HiddenField)ctrlIsShared).Value);
                }

                list.Add(field);
            }

            Resume resume = new Resume();
            foreach (EntityField entityField in list)
            {
                resume.Fields.Add(new Field(entityField.Name, entityField.Value));
            }
            resume.PositionId = new Guid(positionId);
            resume.Id = Guid.NewGuid();
            resume.AdditionalInfo = AdditionalInfoTextArea.Value;
            resume.Address = AddressTextBox.Text;
            resume.ApplicantName = ApplicantNameTextBox.Text;
            resume.ApplicantLastName = ApplicantSurnameTextBox.Text;
            resume.PhoneNumber = PhoneNumberTextBox.Text;
            resume.Date = DateTime.Now;

            if ( (VideoFileUpload.PostedFile != null) && (VideoFileUpload.PostedFile.ContentLength > 0))
            {
                resume.VideoResume = VideoFileUpload.FileBytes;
                resume.IsVideoUploaded = true;
            }

            _presenter.OnSubmit(resume);
            string redirect = ConfigurationManager.AppSettings[Constants.RecruitingHome];
            Response.Redirect(redirect);
        }
    }

    public void DemoOnlyFillBasicApplication(string name, string lastName, string address, string phone, string resume)
    {
        ApplicantNameTextBox.Text = name;
        ApplicantSurnameTextBox.Text = lastName;
        AddressTextBox.Text = address;
        PhoneNumberTextBox.Text = phone;
        AdditionalInfoTextArea.Value = resume;
    }

    protected void DemoLinkButton_Click(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            Presenter.OnDemoLinkClicked();
        }
    }
}
